import request from '../request/index'
import type { MomentDto, MomentVo } from '../types/moment'

export const getMoments = async (params: {
    userId: string
    page: number
    size: number,
    type: 0 | 1
}) => {
    try {
        const res = await request.get<MomentVo[]>('/moment/user', params)
        res.data.forEach((item) => {
            item.location = JSON.parse(item.location as string)
        })
        return res.data
    } catch (err) {
        throw err
    }
}

export const createMoment = async (params: MomentDto) => {
    try {
        return await request.post('/moment', params)
    } catch (err) {
        throw err
    }
}

export const deleteMoment = async (id: string) => {
    try {
        return await request.delete('/moment', { id })
    } catch (err) {
        throw err
    }
}

export const likeMoment = async (id: string) => {
    try {
        const res = await request.put<MomentVo>('/moment/like', { id })
        return res.data
    } catch (err) {
        throw err
    }
}
